#include <stdio.h>
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>

#define MEMORY_SIZE 20

using namespace std;

void printError()
{
	cout << "ERROR: Connection failed" << '\n';
}

int main()
{
    int shmid;
    char *sharedMemory;
    shmid = shmget(2404, MEMORY_SIZE, IPC_CREAT | 0666);

    if (shmid >= 0) 
    {
        sharedMemory = (char*) shmat(shmid, 0, 0);

        if (sharedMemory==(char *)-1) 
		{
            printError();
		}
        else 
        {
            cout << "Please fill in a number: "; 

            float number;
            cin >> number;
            
            sharedMemory[0] = number;
            
			cout << "\n Waiting for second program to calculate....\n";

			while (sharedMemory[0] == number) { }
                        

	    float square = sharedMemory[0];
            cout << "The square of " << number << " is " << square << '\n';
            
            shmdt(sharedMemory);
            shmctl(shmid, IPC_RMID, 0);
        }
    } 
    else 
    {
	printError();
    }
	int a;
	cin >> a;
    
    return 0;
}
